package com.fengmi.service.impl;

import com.fengmi.dto.EntityDTO;
import com.fengmi.entity.MallGoods;
import com.fengmi.entity.MallGoodsSku;
import com.fengmi.entity.ViewGoods;
import com.fengmi.mapper.MallGoodsMapper;
import com.fengmi.mapper.MallGoodsSkuMapper;
import com.fengmi.service.MallGoodsService;
import com.fengmi.vo.GoodsSpuAndSkuVO;
import com.fengmi.vo.InfoResult;
import com.fengmi.vo.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Author: 施俊豪
 * Date: 2022/1/25 21:19
 * Note:
 */
@Transactional
@Service
public class MallGoodsServiceImpl implements MallGoodsService {

    @Autowired(required = false)
    private MallGoodsMapper mallGoodsMapper;
    @Autowired(required = false)
    private MallGoodsSkuMapper mallGoodsSkuMapper;
    /**
     * @Note : 条件+分页查询商品信息
     * @Date : 2022-01-25 21:19:45
     * @Author : 清酒吖
    */
    @Override
    public PageResult findGoodsByPage(EntityDTO<MallGoods> entity) {
        // 分页
        PageHelper.startPage(entity.getPageNum(),entity.getPageSize());
        // 条件查询商品信息
        List<ViewGoods> goodsList = mallGoodsMapper.findGoods(entity.getWhere());
        // 返回pageinfo结果
        PageInfo<ViewGoods> pageInfo = new PageInfo<>(goodsList);
        PageResult<ViewGoods> pageResult = new PageResult();
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setPages(pageInfo.getPages());
        pageResult.setPageList(pageInfo.getList());
        return pageResult;
    }

    /**
     * @Note : 添加商品信息和sku
     * @Date : 2022-02-04 21:50:30
     * @Author : 清酒吖

    */
    @Override
    public InfoResult addGoodsAndSku(GoodsSpuAndSkuVO spuAndSkuVO) {
        if(spuAndSkuVO.getGoods() == null){
            return new InfoResult(false, "参数错误","");
        }
        if(spuAndSkuVO.getSkus() == null){
            return new InfoResult(false, "参数错误","");
        }
        Date date = new Date();
        // 获取商品信息
        MallGoods goods = spuAndSkuVO.getGoods();
        // 获取商品sku信息
        List<MallGoodsSku> skus = spuAndSkuVO.getSkus();
        goods.setCreateTime(date);
        goods.setUpdateTime(date);
        // 添加商品spu数据
        Integer spuRow = mallGoodsMapper.addGoods(goods);
        // 循环添加
        for (MallGoodsSku sku : skus) {
            sku.setSpuId(goods.getSpuId());
            sku.setCategory3Id(Long.toString(goods.getCategory3Id()));
            sku.setCategoryid(goods.getCategory3Id());
            sku.setCreateTime(date);
            sku.setUpdateTime(date);
            sku.setSkuStatus("1");
            // 添加商品sku数据
            Integer skuRow = mallGoodsSkuMapper.addGoodsSku(sku);
        }

        return new InfoResult(true,"添加成功","");
    }

    /**
     * @Note : 批量删除商品spu和sku
     * @Date : 2022-02-05 14:45:08
     * @Author : 清酒吖
    */
    @Override
    public InfoResult delGoodsAndSku(Integer[] ids) {
        if(ids.length <= 0){
            return new InfoResult(false,"参数错误","");
        }
        // 批量删除商品spu
        Integer spuRow = mallGoodsMapper.delGoods(ids);

        // 批量删除商品sku
        Integer skuRow = mallGoodsSkuMapper.delSkus(ids);

        return new InfoResult(true,"删除成功","");

    }

}
